<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>注册</title>
  <link rel="stylesheet" th:href="@{/layui/css/layui.css}">
  <script th:src="@{/layui/layui.js}"></script>
  <script th:src="@{/layui/lay/modules/layer.js}"></script>
  <link rel="stylesheet" th:href="@{/layui/css/modules/layer/default/layer.css}">
<body style="width: 100%;
height:auto;
	background:url(../images/index.jpg) no-repeat top center fixed;
	  -webkit-background-size:cover;

" class="forget">
<div style="margin-top: 15%;">
  <div class="layui-main" style="width: 600px;">
    <h1 style="text-align: center" >忘记密码</h1>
    <form class="layui-form layui-form-pane" method="post">
      <div class="layui-form-item">
        <label class="layui-form-label">邮箱：</label>
        <div class="layui-input-block">
          <input id="userEmail" type="email" name="userEmail" class="layui-input"  lay-verify="userEmail" style="width: 400px"/>
        </div>
      </div>
      <div class="layui-form-item ">
        <label class="layui-form-label">密码：</label>
        <div class="layui-input-block">
          <input id="userPassword" type="password" name="userPassword" class="layui-input form-control" lay-verify="userPassword"  style="width: 400px"/>
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">请再次输入密码：</label>
        <div class="layui-input-block">
          <input id="userPasswordAgain" type="password" name="userPasswordAgain" class="layui-input" lay-verify="userPasswordAgain"  style="width: 400px"/>
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">邮箱验证码：</label>
        <div class="layui-input-block">
          <input id="checkCode" type="text" name="checkCode" class="layui-input" lay-verify="required"  style="width: 400px"/>
          <button id="sendCheckCode" type="button" class="layui-btn layui-btn-normal btn-success btn-block">获取验证码</button>
        </div>
      </div>
      <div class="layui-form-item">
        <div class="layui-input-block">
          <button class="layui-btn btn-success btn-block" lay-submit lay-filter="update">确认</button>
        </div>
        <a href="/toUserLogin" class="" >谢谢，我想起密码了</a>
      </div>

    </form>
  </div>
</div>
<script>
    layui.use("form",function () {
        var form = layui.form;
        var $ = layui.$;

        form.on("submit(update)",function (data) {
            $.ajax({
                url:"/userLogin/forgetPassword",
                type:"POST",
                data:data.field,
                async:false,
                success:function (text) {
                    if ("ok" == text){
                        layer.alert("修改成功",function () {
                            window.location.href = "/";
                        });
                    }else if("error"==text){
                        layer.alert("验证码错误",function () {
                            $('#checkCode').val("");
                        });
                    }else if ( "UnKnowAccount" === text) {
                        layer.msg("用户已存在");
                        window.location.href = "/toForget";
                    }
                }
            });
            return false;
        });

        form.verify({
            userPassword: function(value){
                if(value.length < 6){
                    return '内容请输入至少6个字符';
                }
            },
            userPasswordAgain: function(value){
                if (value != $('#userPassword').val()){
                    return '密码不一致';
                    $('#userPassword').val("");
                    $('#userPasswordAgain').val("");
                }

            },
            userEmail: [/^[a-z0-9._%-]+@([a-z0-9-]+\.)+[a-z]{2,4}$|^1[3|4|5|7|8]\d{9}$/, '邮箱格式不对']
        });
        //验证码
        var checkCode = "";
        $("#sendCheckCode").click(function () {
            var email = $("#userEmail").val();
            if (email == null || email == ""){
                layer.msg("请输入邮箱！！！");
                return;
            }
            var index = layer.open({
                type:3,
                content:"邮件发送中..."
            });

            $.ajax({
                url:"/userLogin/getCheckCode?email="+email,
                type:"get",
                success:function (text) {
                    if (text != null && text != ""){
                        layer.close(index);
                        layer.msg("已发送");
                        checkCode = text;
                        countDown();
                    } else{
                        layer.alert("获取失败，请重新获取")
                    }
                }
            });
        });

        var maxTime = 60;
        function countDown(){
            if (maxTime == 0){
                checkCode = "";
                $("#sendCheckCode").removeClass("layui-btn-disabled");
                $("#sendCheckCode").removeAttr("disabled")
                $("#sendCheckCode").html("获取验证码");
                maxTime = 60;
            }else{
                $("#sendCheckCode").attr("disabled","disabled");
                $("#sendCheckCode").addClass("layui-btn-disabled");
                form.render();
                $("#sendCheckCode").html(maxTime+"秒后重新获取");
                maxTime--;
                setTimeout(countDown,1000);
            }
        }

    });
</script>
</body>
</head>
</html>